package com.zqq.goods.widget

import android.content.Context
import android.util.AttributeSet
import android.util.EventLog
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.eightbitlab.rxbus.Bus
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zqq.goods.R
import com.zqq.goods.event.SkuChangedEvent
import com.zqq.goods.service.bean.GoodsSku
import com.zqq.provider.router.RouterPath
import kotlinx.android.synthetic.main.layout_sku_view.view.*
import org.greenrobot.eventbus.EventBus

/**
 * 单个SKU
 * wc
 * @author zqq on 2019/5/30.
 */
class SkuView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private lateinit var mGoodsSku: GoodsSku

    init {
        View.inflate(context, R.layout.layout_sku_view, this)
    }

    /**
     * 动态设置SKU数据
     */
    fun setSkuData(goodsSku: GoodsSku) {
        mGoodsSku = goodsSku
        mSkuTitleTv.text = goodsSku.skuTitle

        //FlowLayout设置数据(流式布局)---泛型是需要的显示的数据
        mSkuContentView.adapter = object : TagAdapter<String>(goodsSku.skuContent) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                val view: TextView = LayoutInflater.from(context)
                        .inflate(R.layout.layout_sku_item, parent, false) as TextView

                //设置显示数据
                view.text = t
                return view
            }
        }

        mSkuContentView.adapter.setSelectedList(0)
        mSkuContentView.setOnTagClickListener { view, position, parent ->
//            Bus.send(SkuChangedEvent())
            EventBus.getDefault().post(SkuChangedEvent())
            true
        }
    }

    /**
     * 获取选中的SKU
     */
    fun getSkuInfo(): String {
        return mSkuTitleTv.text.toString() + RouterPath.GoodsCenter.SKU_SEPARATOR +
                mGoodsSku.skuContent[mSkuContentView.selectedList.first()]
    }

}